package com.example.testsvn;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnInitListener {
	private TextToSpeech speaker;
	private Button bouton, btnChangeActivity;
	private EditText editText;
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		speaker = new TextToSpeech(this, this);
		bouton = (Button) findViewById(R.id.button1);
		btnChangeActivity = (Button) findViewById(R.id.button2);
		editText = (EditText) findViewById(R.id.editText1);
		imageView = (ImageView) findViewById(R.id.imageView1);

		// Uri uriImage = Uri.parse("file:///mnt/sdcard/Images/tux.png");
		// imageView.setImageURI(uriImage);

		imageView.setImageResource(R.drawable.tux);

		imageView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String text = "salut";
				speak(text);
			}
		});

		bouton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String text = editText.getText().toString();
				speak(text);
			}
		});

		btnChangeActivity.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						TestActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	private void speak(String text) {
		speaker.speak(text, TextToSpeech.QUEUE_FLUSH, null);
	}

	@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = speaker.setLanguage(Locale.FRANCE);

			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				Log.e("TextToSpeech", "Unsupported Language");
			}
		} else {
			Log.e("TextToSpeech", "Initialization failed");
		}
	}

}
